package com.tender.common;

import cn.hutool.http.HttpStatus;
import lombok.Data;
import com.tender.constants.Constants;

/**
 * 结果包装类
 */
@Data
public class Result<T> {

    private Integer code;

    private String msg;

    private T data;

    private boolean encrypted = false;

    /* ============== success ============ */

    public static <T> Result<T> success() {
        Result<T> jsonData = new Result<>();
        jsonData.setCode(HttpStatus.HTTP_OK);
        jsonData.setMsg(Constants.OK);

        return jsonData;
    }

    public static <T> Result<T> success(String msg) {
        Result<T> result = success();
        result.setMsg(msg);

        return result;
    }

    public static <T> Result<T> success(T object) {
        Result<T> result = success();
        result.setData(object);

        return result;
    }

    public static <T> Result<T> success(String msg, T object) {
        Result<T> result = success();
        result.setData(object);
        result.setMsg(msg);

        return result;
    }

    /* ============== fail ============ */

    public static <T> Result<T> fail(Integer code, String msg) {
        Result<T> result = fail(msg);
        result.setCode(code);

        return result;
    }

    public static <T> Result<T> fail(String msg, T data) {
        Result<T> result = fail(msg);
        result.setData(data);

        return result;
    }

    public static <T> Result<T> fail(String msg) {
        Result<T> result = new Result<>();
        result.setMsg(msg);
        result.setCode(HttpStatus.HTTP_BAD_REQUEST);

        return result;
    }

}
